package com.tangyh.lamp.common.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 数字转RMB大写
 */

public class Amount2RMB {
    private static final char[] CN_UPPER_NUMBER = "零壹贰叁肆伍陆柒捌玖".toCharArray();
    private static final char[] CN_UPPER_UNIT = "仟佰拾".toCharArray();
    private static final char[] CN_GROUP = "圆万亿".toCharArray();

    public static String moneyToChinese(String money) {
        return moneyToChinese(new BigDecimal(money.replaceAll(",", "")));
    }

    public static String moneyToChinese(double money) {
        return moneyToChinese(new BigDecimal(money));
    }

    public static String moneyToChinese(BigDecimal money) {
        if (new BigDecimal("0.00").add(money).equals(new BigDecimal("0.00"))) {
            return "零圆整";
        }
        double max = 1000000000000D;
        double min = 0.01D;
        if (money.doubleValue() >= max || money.doubleValue() < min) {
            return "大于1万亿或小于1分了";
        }
        money = money.setScale(2, RoundingMode.HALF_UP);
        String numStr = money.toString();
        int pointPos = numStr.indexOf('.');
        //整数部分
        String s_int;
        //小数部分
        String s_point = null;
        if (pointPos >= 0) {
            s_int = numStr.substring(0, pointPos);
            s_point = numStr.substring(pointPos + 1);
        } else {
            s_int = numStr;
        }
        StringBuilder sb = new StringBuilder();
        if (!"0".equals(s_int)) {
            int groupCount = (int) Math.ceil(s_int.length() / 4.0);
            for (int group = 0; group < groupCount; group++) {
                boolean zeroFlag = true;
                boolean noZeroFlag = false;
                int start = (s_int.length() % 4 == 0 ? 0 : (s_int.length() % 4 - 4)) + 4 * group;
                for (int i = 0; i < 4; i++) {
                    if (i + start >= 0) {
                        int value = s_int.charAt(i + start) - '0';
                        if (value > 0) {
                            sb.append(CN_UPPER_NUMBER[value]);
                            if (i < 3) {
                                sb.append(CN_UPPER_UNIT[i]);
                            }
                            zeroFlag = true;
                            noZeroFlag = true;
                        } else if (zeroFlag) {
                            sb.append('零');
                            zeroFlag = false;
                        }
                    }
                }
                if (sb.charAt(sb.length() - 1) == '零') {
                    sb.deleteCharAt(sb.length() - 1);
                }
                if (noZeroFlag || groupCount - group == 1) {
                    sb.append(CN_GROUP[groupCount - group - 1]);
                }
            }
        }
        if (s_point == null || "00".equals(s_point)) {
            sb.append('整');
        } else {
            int j = s_point.charAt(0) - '0';
            int f = s_point.charAt(1) - '0';
            if (j > 0) {
                sb.append(CN_UPPER_NUMBER[j]).append('角');
                if (f != 0) {
                    sb.append(CN_UPPER_NUMBER[f]).append('分');
                }
            } else if ("0".equals(s_int)) {
                sb.append(CN_UPPER_NUMBER[f]).append('分');
            } else {
                sb.append('零').append(CN_UPPER_NUMBER[f]).append('分');
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        System.out.println(Amount2RMB.moneyToChinese(new BigDecimal("999999999999.99")));

        System.out.println(Amount2RMB.moneyToChinese("0"));
        System.out.println(Amount2RMB.moneyToChinese("0.0"));
        System.out.println(Amount2RMB.moneyToChinese("0.00"));
        System.out.println(Amount2RMB.moneyToChinese("16,409.02"));
        System.out.println(Amount2RMB.moneyToChinese("1,409.50"));
        System.out.println(Amount2RMB.moneyToChinese("6,007.14"));
        System.out.println(Amount2RMB.moneyToChinese("1,680.32"));
        System.out.println(Amount2RMB.moneyToChinese("325.04"));
        System.out.println(Amount2RMB.moneyToChinese("4,321.00"));
        System.out.println(Amount2RMB.moneyToChinese("0.01"));

        System.out.println(Amount2RMB.moneyToChinese("1234,5678,9012.34"));
        System.out.println(Amount2RMB.moneyToChinese("1000,1000,1000.10"));
        System.out.println(Amount2RMB.moneyToChinese("9009,9009,9009.99"));
        System.out.println(Amount2RMB.moneyToChinese("5432,0001,0001.01"));
        System.out.println(Amount2RMB.moneyToChinese("1000,0000,1110.00"));
        System.out.println(Amount2RMB.moneyToChinese("1010,0000,0001.11"));
        System.out.println(Amount2RMB.moneyToChinese("1000,0000,0000.01"));

        System.out.println(System.currentTimeMillis() - l);
    }
}
